package com.frame.pageform.tag;

import java.lang.reflect.Array;
import java.util.*;

import com.frame.database.JdataBean;
import com.frame.metadata.wordbook.WordbookService;
import com.frame.pageform.Iterate;
import com.frame.pageform.PageBeanUtil;
import com.frame.util.DataConvert;

public class OptionsTag extends BaseHandlerTag{

	private String bind;
	private String multiple;
	private String match[];
	private String collection;
	private String text;
	private String value;
	private String dict;

	public int start() throws Exception {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
		match=null;
		if(bind!=null && !"".equals(bind)){
			Object obj=executeExpression(bind);
			prepareClick(sb);
			prepareHandler(sb);
			if(obj!=null){
				if(obj.getClass().isArray()){
					try{
						int n=Array.getLength(obj);
						match=new String[n];
						for(int i=0;i<Array.getLength(obj);i++){
							match[i]=Array.get(obj, i).toString();
						}
					}catch(Exception e){throw new Exception(bind+"不支持多层复杂的数组结构数据",e);}
				}else if(obj instanceof List){
					List list=(List)obj;
					match=(String[])list.toArray(new String[list.size()]);
				}
				else if(obj instanceof Map){
					try{
						Map map=(Map)obj;
						Set set=map.keySet();
						Object[] objs=(Object[])set.toArray().clone();
						match=new String[objs.length];
						for(int i=0;i<objs.length;i++){
							match[i]=map.get(objs[i]).toString();
						}
					}catch(Exception e){throw new Exception(bind+"不支持多层复杂的Map结构数据",e);}
				}
				else
					match=new String[]{obj.toString()};

				if(match == null)
					match = new String[0];
			}
		}
		doCollection(sb);
		write(sb.toString());
		return 2;
	}
	public int end() throws Exception
	{

		return 0;
	}
	protected void doCollection(StringBuffer sb) throws Exception{
		Object collectionList=null;
		if(collection!=null && !"".equals(collection))
			collectionList = executeExpression(collection);
		else if(dict!=null && !"".equals(dict)){
			collectionList=WordbookService.getIntance().queryItem(dict);
			this.value="value";
			this.text="text";
		}
		if(collectionList!=null){
			Iterator iterator =Iterate.getIterator(collectionList);
			int index = 0;
			while(iterator.hasNext())
			{
				Object obj = iterator.next();
				if(obj!=null){
					Object value = null;
					Object text = null;
					if(collectionList instanceof Map)
					{
						value = ((Map.Entry)obj).getKey();
						text = ((Map.Entry)obj).getValue();
					}
					else
					{
						if(obj instanceof Map){
							value=((Map)obj).get(this.value);
							text=((Map)obj).get(this.text);
						}else if(obj instanceof JdataBean){
							value=((JdataBean)obj).get(this.value);
							text=((JdataBean)obj).get(this.text);
						}else{
							if(this.value != null)
								value = PageBeanUtil.getOgnlValue(this.value,obj,null);
							else
								value = obj;

							if(this.text != null)
								text = PageBeanUtil.getOgnlValue(this.text,obj,null);
							else
								text = obj;
						}
					}
					if(value == null)
						value = "";

					if(text == null)
						text = "";

					String valueStr = DataConvert.toString(value);
					String textStr= DataConvert.toString(text);

					addOption(sb, textStr, valueStr, isMatched(value.toString()));
					index++;
				}
			}
		}

	}

	protected void addOption(StringBuffer sb,String text,String value,boolean selected){
		sb.append("<option value=\"");
		sb.append(value);
		sb.append("\"");
		if(selected)
			sb.append(" selected=\"selected"+"\"");
		sb.append(">");
		sb.append(text);
		sb.append("</option>\r\n");
	}
	private boolean isMatched(String value)
	{
		if(match == null || value == null)
			return false;
		for(int i = 0; i < match.length; i++)
			if(value.equals(match[i]))
				return true;

		return false;
	}
	public String getBind() {
		return bind;
	}

	public void setBind(String bind) {
		this.bind = bind;
	}

	public String getMultiple() {
		return multiple;
	}

	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}

	public String getCollection() {
		return collection;
	}
	public void setCollection(String collection) {
		this.collection = collection;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getDict() {
		return dict;
	}
	public void setDict(String dict) {
		this.dict = dict;
	}

}
